<template>
  <div>
    <el-table :data="tableData" stripe style="width: 100%" max-height="600">
      <el-table-column prop="orderBy" label="排序" width="60">
      </el-table-column>
      <el-table-column prop="name" label="名称" show-overflow-tooltip>
      </el-table-column>
      <el-table-column
        prop="code"
        label="编码"
        v-if="$store.getters.userId < 0"
      >
      </el-table-column>
      <!-- <el-table-column prop="status" label="规则类型" width="100">
        <template slot-scope="scope">
          {{ ruleTypeFilter(scope.row.ruleType) }}
        </template>
      </el-table-column> -->
      <el-table-column prop="value" label="值" width="120"> </el-table-column>
      <el-table-column prop="status" label="状态" width="120">
        <template slot-scope="scope">
          <span class="color-success" v-show="scope.row.status == 0">开启</span>
          <span v-show="scope.row.status == 1">关闭</span>
        </template>
      </el-table-column>
      <!-- <el-table-column prop="status" label="状态" width="120">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            active-value="0"
            inactive-value="1"
            @change="handleStatus(scope.row)"
          >
          </el-switch>
        </template>
      </el-table-column> -->
      <el-table-column prop="createTime" label="创建时间" width="180">
      </el-table-column>
      <el-table-column label="操作" width="140">
        <template slot-scope="{ row }">
          <el-button-group>
            <el-button type="primary" size="mini" @click="handleEdit(row)"
              >编辑</el-button
            >
            <el-button
              v-if="$store.getters.userId < 0"
              type="danger"
              size="mini"
              @click="handleDelete(row)"
              >删除</el-button
            >
          </el-button-group>
        </template>
      </el-table-column>
      <div slot="empty" class="emptyBg">
        <el-empty :image-size="200"></el-empty>
      </div>
    </el-table>
    <Pagination
      v-show="parseInt(pageObj.count) > 0"
      :total="parseInt(pageObj.count)"
      @change="getList"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
    />
  </div>
</template>
<script>
import {
  sysScoreRuleQuery,
  sysScoreRuleDelete,
  sysScoreRuleUpdate,
} from '@/api/admin/system/sysScoreRule'
// import { debounce } from 'lodash'
export default {
  data() {
    return {
      listQuery: {
        page: 1,
        limit: 10,
      },
      tableData: [],
      pageObj: {},
    }
  },
  props: ['ruleList'],
  // computed: {
  //   ruleTypeFilter() {
  //     return function (type) {
  //       return this.ruleList.filter((v) => v.value == type)[0].msg
  //     }
  //   },
  // },
  mounted() {
    this.getList()
    this.$bus.$on('refreshScoreRuleList', (v) => {
      if (v) {
        Object.assign(this.listQuery, v)
      }
      this.getList()
    })
  },
  beforeDestroy() {
    this.$bus.$off('refreshScoreRuleList')
  },
  methods: {
    getList() {
      sysScoreRuleQuery(this.listQuery).then((res) => {
        this.tableData = res.data.data
        this.pageObj = res.data
      })
    },
    //发布
    // handleStatus: debounce(function (v) {
    //   let { id, status } = v
    //   sysScoreRuleUpdate({ id, status }).then((res) => {
    //     this.getList()
    //     if (res.data.code != 0) return
    //     this.$message({
    //       message: '操作成功',
    //       type: 'success',
    //     })
    //   })
    // }, 500),
    //编辑
    handleEdit(row) {
      this.$bus.$emit('showDialogScoreRuleAdd', row)
    },
    //删除确认
    handleDelete(row) {
      this.$confirm('确定要删除吗?', '提示', {
        confirmButtonText: '删除',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          this.delete(row)
        })
        .catch(() => {})
    },
    //删除
    delete(row) {
      sysScoreRuleDelete(row.id).then((res) => {
        this.getList()
        this.$message({
          message: '操作成功',
          type: 'success',
        })
      })
    },
  },
}
</script>